﻿using System;
using System.Windows;
using System.Windows.Controls;
using WgpgCore;

namespace WgpgGUI
{
    public partial class CustomMessageBox : Window
    {
        private MessageBoxResult m_result = MessageBoxResult.None;
        private Button m_close;

        #region Constructors
        public CustomMessageBox()
        {
            InitializeComponent();
        }
        #endregion

        #region Properties
        public String Caption
        {
            get { return Title; }
            set { Title = value; }
        }

        public MessageBoxResult MessageBoxResult
        {
            get { return m_result; }
            private set
            {
                m_result = value;
                DialogResult = m_result != MessageBoxResult.Cancel;
            }
        }
        #endregion

        #region Dependency Properties
        public static readonly DependencyProperty DefaultResultProperty =
            DependencyProperty.Register("DefaultResult", typeof(MessageBoxResult),
                typeof(CustomMessageBox), new UIPropertyMetadata(MessageBoxResult.None));

        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register("Message", typeof(String),
                typeof(CustomMessageBox), new UIPropertyMetadata(String.Empty));

        public static readonly DependencyProperty MessageBoxButtonProperty =
            DependencyProperty.Register("MessageBoxButton", typeof(MessageBoxButton),
                typeof(CustomMessageBox), new UIPropertyMetadata(MessageBoxButton.OK));

        public static readonly DependencyProperty MessageBoxImageProperty =
            DependencyProperty.Register("MessageBoxImage", typeof(MessageBoxImage),
                typeof(CustomMessageBox), new UIPropertyMetadata(MessageBoxImage.None));

        public static readonly DependencyProperty OkButtonCaptionProperty =
            DependencyProperty.Register("OkButtonCaption", typeof(String),
                typeof(CustomMessageBox), new UIPropertyMetadata("_OK"));

        public static readonly DependencyProperty YesButtonCaptionProperty =
            DependencyProperty.Register("YesButtonCaption", typeof(String),
                typeof(CustomMessageBox), new UIPropertyMetadata("_Yes"));

        public static readonly DependencyProperty NoButtonCaptionProperty =
            DependencyProperty.Register("NoButtonCaption", typeof(String),
                typeof(CustomMessageBox), new UIPropertyMetadata("_No"));

        public static readonly DependencyProperty CancelButtonCaptionProperty =
            DependencyProperty.Register("CancelButtonCaption", typeof(String),
                typeof(CustomMessageBox), new UIPropertyMetadata("_Cancel"));

        public static readonly DependencyProperty EscapeButtonProperty =
            DependencyProperty.Register("EscapeButton", typeof(MessageBoxResult),
                typeof(CustomMessageBox), new UIPropertyMetadata(MessageBoxResult.None));

        public MessageBoxResult EscapeButton
        {
            get { return (MessageBoxResult)GetValue(EscapeButtonProperty); }
            set
            {
                SetValue(EscapeButtonProperty, value);

                _ok.IsCancel = false;
                _yes.IsCancel = false;
                _no.IsCancel = false;
                _cancel.IsCancel = false;

                switch (value)
                {
                    case MessageBoxResult.OK:
                        _ok.IsCancel = true;
                        break;
                    case MessageBoxResult.Yes:
                        _yes.IsCancel = true;
                        break;
                    case MessageBoxResult.No:
                        _no.IsCancel = true;
                        break;
                    case MessageBoxResult.Cancel:
                        _cancel.IsCancel = true;
                        break;
                }
            }
        }

        public String OkButtonCaption
        {
            get { return (String)GetValue(OkButtonCaptionProperty); }
            set
            {
                SetValue(OkButtonCaptionProperty, value);
                _ok.Content = value;
            }
        }

        public String YesButtonCaption
        {
            get { return (String)GetValue(YesButtonCaptionProperty); }
            set
            {
                SetValue(YesButtonCaptionProperty, value);
                _yes.Content = value;
            }
        }

        public String NoButtonCaption
        {
            get { return (String)GetValue(NoButtonCaptionProperty); }
            set
            {
                SetValue(NoButtonCaptionProperty, value);
                _no.Content = value;
            }
        }

        public String CancelButtonCaption
        {
            get { return (String)GetValue(CancelButtonCaptionProperty); }
            set
            {
                SetValue(CancelButtonCaptionProperty, value);
                _cancel.Content = value;
            }
        }

        public MessageBoxResult DefaultResult
        {
            get { return (MessageBoxResult)GetValue(DefaultResultProperty); }
            set
            {
                SetValue(DefaultResultProperty, value);

                switch (value)
                {
                    case MessageBoxResult.Cancel:
                        m_result = MessageBoxResult.Cancel;
                        _cancel.IsDefault = true;
                        break;
                    case MessageBoxResult.No:
                        m_result = MessageBoxResult.No;
                        _no.IsDefault = true;
                        break;
                    case MessageBoxResult.None:
                        m_result = MessageBoxResult.None;
                        break;
                    case MessageBoxResult.OK:
                        m_result = MessageBoxResult.OK;
                        _ok.IsDefault = true;
                        break;
                    case MessageBoxResult.Yes:
                        m_result = MessageBoxResult.Yes;
                        _yes.IsDefault = true;
                        break;
                    default:
                        break;
                }
            }
        }

        public String Message
        {
            get { return (String)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }

        public MessageBoxButton MessageBoxButton
        {
            get { return (MessageBoxButton)GetValue(MessageBoxButtonProperty); }
            set
            {
                SetValue(MessageBoxButtonProperty, value);

                switch (value)
                {
                    case MessageBoxButton.OK:
                        _ok.Visibility = Visibility.Visible;
                        break;
                    case MessageBoxButton.OKCancel:
                        _ok.Visibility = Visibility.Visible;
                        _cancel.Visibility = Visibility.Visible;
                        break;
                    case MessageBoxButton.YesNo:
                        _yes.Visibility = Visibility.Visible;
                        _no.Visibility = Visibility.Visible;
                        break;
                    case MessageBoxButton.YesNoCancel:
                        _yes.Visibility = Visibility.Visible;
                        _no.Visibility = Visibility.Visible;
                        _cancel.Visibility = Visibility.Visible;
                        break;
                    default:
                        break;
                }
            }
        }

        public MessageBoxImage MessageBoxImage
        {
            get { return (MessageBoxImage)GetValue(MessageBoxImageProperty); }
            set
            {
                SetValue(MessageBoxImageProperty, value);
                _image.Visibility = value == MessageBoxImage.None ? Visibility.Collapsed : Visibility.Visible;
            }
        }
        #endregion

        #region Static Methods
        public static MessageBoxResult Show(String messageBoxText)
        {
            return Show(null, messageBoxText, String.Empty, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None);
        }

        public static MessageBoxResult Show(String messageBoxText, String caption)
        {
            return Show(null, messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None);
        }

        public static MessageBoxResult Show(Window owner, String messageBoxText)
        {
            return Show(owner, messageBoxText, String.Empty, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None);
        }

        public static MessageBoxResult Show(String messageBoxText, String caption, MessageBoxButton button)
        {
            return Show(null, messageBoxText, caption, button, MessageBoxImage.None, MessageBoxResult.None);
        }

        public static MessageBoxResult Show(Window owner, String messageBoxText, String caption)
        {
            return Show(owner, messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None);
        }

        public static MessageBoxResult Show(String messageBoxText, String caption, MessageBoxButton button, MessageBoxImage icon)
        {
            return Show(null, messageBoxText, caption, button, icon, MessageBoxResult.None);
        }

        public static MessageBoxResult Show(Window owner, String messageBoxText, String caption, MessageBoxButton button)
        {
            return Show(owner, messageBoxText, caption, button, MessageBoxImage.None, MessageBoxResult.None);
        }

        public static MessageBoxResult Show(String messageBoxText, String caption, MessageBoxButton button, MessageBoxImage image, MessageBoxResult defaultResult)
        {
            return Show(null, messageBoxText, caption, button, image, defaultResult);
        }

        public static MessageBoxResult Show(Window owner, String messageBoxText, String caption, MessageBoxButton button, MessageBoxImage icon)
        {
            return Show(owner, messageBoxText, caption, button, icon, MessageBoxResult.None);
        }

        public static MessageBoxResult Show(Window owner, String messageBoxText, String caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, String okCaption = null, String yesCaption = null, String noCaption = null, String cancelCaption = null, MessageBoxResult escapeButton = MessageBoxResult.None)
        {
            CustomMessageBox messageBox = new CustomMessageBox
            {
                Caption = caption,
                DefaultResult = defaultResult,
                Owner = owner,
                Message = messageBoxText,
                MessageBoxButton = button,
                MessageBoxImage = icon,
                EscapeButton = escapeButton
            };

            if (okCaption != null)
                messageBox.OkButtonCaption = okCaption;

            if (yesCaption != null)
                messageBox.YesButtonCaption = yesCaption;

            if (noCaption != null)
                messageBox.NoButtonCaption = noCaption;

            if (cancelCaption != null)
                messageBox.CancelButtonCaption = cancelCaption;

            if (messageBox.ShowDialog() == false)
                return messageBox.EscapeButton;

            return messageBox.MessageBoxResult;
        }
        #endregion

        #region Event Handlers
        protected override void OnSourceInitialized(EventArgs e)
        {
            Utils.RemoveIcon(this);
        }

        private void cancel_Click(Object sender, RoutedEventArgs e)
        {
            MessageBoxResult = MessageBoxResult.Cancel;
        }

        private void no_Click(Object sender, RoutedEventArgs e)
        {
            MessageBoxResult = MessageBoxResult.No;
        }

        private void ok_Click(Object sender, RoutedEventArgs e)
        {
            MessageBoxResult = MessageBoxResult.OK;
        }

        private void this_Loaded(Object sender, RoutedEventArgs e)
        {
            m_close = (Button)Template.FindName("PART_Close", this);

            if (m_close != null)
                if (_cancel.IsVisible == false)
                    m_close.IsCancel = false;
        }

        private void yes_Click(Object sender, RoutedEventArgs e)
        {
            MessageBoxResult = MessageBoxResult.Yes;
        }
        #endregion
    }
}
